import json
import os
import sys
from random import random

from Utils import IOUtil
from Utils.TimeUtil import *
from Service.MyLogger import MyLogger
import os,logging
logFileName =  os.path.basename(__file__).split(".")[0]
from Statistics.COMMON import commonLogger
log = commonLogger # MyLogger(r"./Logs/%s.log" % (logFileName), logging.DEBUG, logging.DEBUG)
def toBatchRefundTxt(filePaths):
    filePathArr=filePaths.strip('\n').split('\n')
    begStr = "交易金额(元)	"
    needRefundStr = "买家已支付"
    for filePath in filePathArr:
        path, fileName = os.path.split(filePath)
        path = path.strip('\n')
        fileName = fileName.strip('\n')
        (fileNa, extension) = os.path.splitext(fileName)
        foo = open(filePath,'r',encoding='gbk',errors='ignore')
        ss = foo.read()
        foo.close()
        ss = ss[ss.find(begStr)+len(begStr):]
        ss = ss.strip('\n')
        narr = []
        log.info(ss)
        for arow in ss.split('\n'):
            if arow.find(needRefundStr)>=0:
                arowArr = arow.split('\t')
                narr.append(arowArr[2][1:]+","+arowArr[5]+",,")
        foo = open(path+"/BatchRefund_"+getFormatTimeNow("%m%d%H%M%S")+"_"+getMilliSecondNow()+".txt",'w',
                   encoding='gbk',errors='ignore')
        foo.write('\n'.join(narr))
        foo.close()
def getColNumByName(ss="",arr=[],isSaveToClipBoard=False):
    if arr!=None and len(arr)>0:
        aa = arr
    else:
        if ss=="":
            ss="userId	reqTime	symbol	amount	ts	direction	span	startPrice	endPrice	intentionTime	ratio	grossProfit	endTime	type	orderId	money1	money2	money3	money4	resTime1	resTime2"
        aa = ss.split("\t")
    dictt = {}
    for i in range(len(aa)):
        dictt[aa[i]]=i
    nss = str(dictt)
    if isSaveToClipBoard:
        IOUtil.set_text(nss)
    # print(nss)
    return dictt
def getHeaders(ss="",isSaveToClipBoard=False):
    if ss=="":
        ss='''
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN
Connection: keep-alive
Content-Length: 79
Content-Type: application/json;charset=UTF-8
dark-mode: 0
device-info: d922f9f9edc1617d67d3aa92b39b9c06
device-type: h5
Host: 192.168.31.160:10000
Origin: http://192.168.31.160
Referer: http://192.168.31.160/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36      '''
        aa = ss.split('\n')
        naa = []
        for i in range(len(aa)):
            if aa[i].find(": ")>=0:
                naa.append("'"+aa[i].split(": ")[0]+"'"+": "+"'"+aa[i].split(": ")[1]+"',")
        nss = '\n'.join(naa)
        if isSaveToClipBoard:
            IOUtil.set_text(nss)
        print(nss)
fieldsStr = "userId	reqTime	symbol	amount	ts	direction	span	startPrice	endPrice	intentionTime	ratio	grossProfit	endTime	type	orderId	money1	money2	money3	money4	resTime1	resTime2"
def fieldsStr2dict(fields:str):
    fieldsArr = fieldsStr.strip('\t').split('\t')
    dictStr = ""
    for i in range(len(fieldsArr)):
        ee = fieldsArr[i]
        dictStr = dictStr + "'"+ee+"': "+ee+","
    if len(dictStr)>0:
        dictStr = dictStr[0:len(dictStr)-1]
    dictStr = "{" + dictStr + "}"
    print(dictStr)
    return dictStr
if __name__ == '__main__':
    pass
    # if hasattr(sys, 'frozen'):
    #     application_path = os.path.dirname(sys.executable)
    # elif __file__:
    #     application_path = os.path.dirname(os.path.abspath(__file__))
    # CONFIG.application_path = application_path
    # log.info("yzlog application_path:", application_path)
    # log.info("yzlog CONFIG.application_path:", CONFIG.application_path)
    # ffprobe_cmd = 'ffmpeg -i '
    # ffprobe_cmd = ffprobe_cmd.replace("ffmpeg", '"' + CONFIG.application_path + '\\ffmpeg"')
    # log.info("yzlog ffprobe_cmd:", ffprobe_cmd)
    # fieldsStr2dict("id	order_id	status	ts	user_id	amount	symbol	direction	buy_time	span	buy_price	settle_price	create_time	update_time	del_flag	remark")
    # getHeaders()
    getColNumByName()
    # print(random())